const fs = require("fs");
const xlsx = require("node-xlsx");

const workSheetsFromBuffer = xlsx.parse(
  "./excelFiles/allData.xlsx"
);

// 将同样卡号的数据合并统计
const filter = (obj) => {
  return obj.map((currentValue, index) => {
    let objMaps = {};
    currentValue.data.map((v, i) => {
      let key = v[0];
      if (!objMaps[key]) {
        objMaps[key] = [...v];
      } else {
        objMaps[key] = [...objMaps[key], v[11]];
      }
    });
    // console.log("objMaps: ", objMaps);
    let cache = [];
    Object.keys(objMaps).forEach(key1 => {
      cache.push(objMaps[key1])
    })
    return {
      name: currentValue.name,
      data: cache,
    };
  });
};

// 生成表格数据对象  wch为设置列宽度
const buffer = xlsx.build(filter(workSheetsFromBuffer), {
  "!cols": [{ wch: 50 }, { wch: 50 }],
});

//生成excel
fs.writeFile(
  "./jsonFiles/allData1.xlsx",
  buffer,
  (err) => {
    if (err) throw err;
    console.log("生成完毕");
  }
);
